<template>

    <div class="container">
            <div class="bg">

            </div>

            <div class="content">
                <div class="loginBox">
                    <div class="Logo">
                        <span>VirtualChat - IMM</span>
                        <img src="/systemImage/Immortality.png" style="max-width: 15%;" alt="logo">
                    </div>
                    <div class="InputBoard">
                        <el-form class="logForm"
                            ref="formRef"
                            :model="logInput"
                            :rules="loginRule"
                            label-width="auto"
                        >
                            <el-form-item label="用户名" prop="username">
                                <el-input v-model="logInput.username"></el-input>
                            </el-form-item>

                            <el-form-item label="密码" prop="password">
                                <el-input v-model="logInput.password" type="password"></el-input>
                            </el-form-item>

                            <div style="display: flex;justify-content: center;gap: 35%;margin-top: 10%;">
                                <button type="button" class="textButton" @click="LogIn()">登录</button>
                                <button type="button" class="textButton">注册</button>
                            </div>


                        </el-form>
                    </div>
                    <div class="footer">
                        <meta>VirtualChat - Immortality 2025</meta>
                    </div>

                </div>
            </div>
    </div>
</template>

<script lang="ts" setup>
import {ref} from 'vue'
import type { Login,Sign } from '../common/userLoginSign';
import request from '../utils/request';
import { useUsersStore } from '../stores/usersStore';
import { useStatusStore } from '../stores/statusStore';
import router from '../routers';

const UserStore=useUsersStore();
const StatusStore=useStatusStore();

const formRef=ref()
const logInput=ref<Login>({
    username:'',
    password:''
})
const loginRule={
    username:[
        {   required:true,
            message:'请输入用户名',
            trigger:'change'
        },

        {   pattern:/^[a-zA-Z0-9]{3,30}$/,
            message:'用户名长度在3~30间且不能包含特殊字符',
            trigger:'change'
        }
    ],
    password:[
        {   required:true,
            message:'请输入密码',
            trigger:'change'
        },
        {   pattern:/^[a-zA-Z0-9]{6,20}$/,
            message:'密码长度在6~20间且不能包含特殊字符',
            trigger:'change'
        }
    ]
}

async function LogIn(){
    try{

        await formRef.value.validate();
        const result = await request.post('/api/user/logIn',{username:logInput.value.username,password:logInput.value.password});
        StatusStore.SetToken(result.token);
        localStorage.setItem('userInfo',JSON.stringify(result.UserInfo))
        UserStore.UpdateCurrentUser(result.UserInfo);

        router.push('conversation');

    }catch(error:any){
        console.error('操作错误',error)
    }
    
    
}

</script>

<style scoped>

.container{
    width: 100%;
    height: 100vh;
    position:relative
}

.bg{
    position: absolute;
    z-index:1;
    top:0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgb(0, 0, 0,0.9);
}

.content{
    position: relative;
    z-index: 5;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0);

}

.loginBox{
    position: absolute;
    display: flex;
    flex-direction: column;
    width: 25%;
    height: 60%;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: rgba(0, 0, 0, 0.8);
    border-radius: 15px;
    gap: 1px;
}

.Logo{
    flex: 2;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 150%;
    font-weight: 600;
    color: rgb(255, 255, 255);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.InputBoard{
    flex: 7;
    margin: 5px;
    display: flex;
    justify-content: center;
    
}

.logForm{
    width: 60%;
    margin-top: 10%;
    display: flex;
    flex-direction: column;
    gap: 8%;

}

.footer{
    flex: 1;
    color: rgb(255, 255, 255,0.6);
    font-size: 80%;
    display: flex;
    justify-content: center;
    align-items: center;
    
}

</style>